package space.panker916.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import space.panker916.service.DataService;

import java.util.Date;

@Controller
public class DataController {
    @Autowired
    private DataService dateService;

    // 进入到统计的页面（请求在转发的过程中请求的类型是不变的）
    @RequestMapping(path = "/data", method = {RequestMethod.GET, RequestMethod.POST})
    public String getDataPage() {
        return "/site/admin/data";
    }

    // 统计网站 UV
    @PostMapping("/data/uv")
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                        Model model) {
        long uv = dateService.calculateUV(startDate, endDate);

        model.addAttribute("uvResult", uv);
        model.addAttribute("uvStartDate", startDate);
        model.addAttribute("uvEndDate", endDate);

        return "/site/admin/data";
        // 这里也可以写成：return "forward:/data"
        // 将请求转发到 getDataPage() 方法中去处理，效果是一样的hh
    }

    @PostMapping("/data/dau")
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                         Model model) {
        long dau = dateService.calculateDAU(startDate, endDate);

        model.addAttribute("dauResult", dau);
        model.addAttribute("dauStartDate", startDate);
        model.addAttribute("dauEndDate", endDate);

        return "/site/admin/data";
    }
}
